<?php

namespace backend\controllers;

use Codeception\Module\Redis;
use Yii;
use yii\data\Pagination;
use yii\helpers\Url;
use yii\web\Controller;

class CommonController extends Controller
{

    //项目在服务器的绝对根路径
    public $absBasePath = '';
    //项目的文件相对路径的地址，比如为/advanced/backend/web
    public $url_base = '';
    //vender的相对路径
    public $rel_vender_path = '';
    //vender的adminlte的浏览器的adminlte的相对路径
    public $adminlte = '';
    //vender的adminlte的浏览器的组建的相对路径
    public $rel_vaabc_path = '';
    public $redis;//可以为redis或者为缓存

    /**
     * 权限的之前进行权限的验证，没有权限的不能访问相应的模块
     *
     * {@inheritdoc}
     *
     * @see \yii\web\Controller::beforeAction()
     */
    public function beforeAction($action)
    {
        date_default_timezone_set('PRC');//设置时间时区为中国
        header("Content-type: text/html; charset=utf-8");//设置格式为utf-8
        // 获取 redis 组件
        //$this->redis = Yii::$app->redis;
        $this->redis = Yii::$app->cache;
        $this->parmsInit();
        $action = Yii::$app->controller->action->id;
        $controller = Yii::$app->controller->id;
        $permissionName = '/' . $controller . '/' . $action;
// 		if (!Yii::$app->user->can($permissionName) 
// 				&& Yii::$app->getErrorHandler()->exception === null 
// 				&& !Yii::$app->user->isGuest) {
// 			//有权限访问该模块
// 			//表示没有权限访问该页面的模块，显示错误的页面
// 			ob_start();
// 			ob_clean();
// 			echo $this->render('@app/views/common/error');
// 			flush();
// 			exit();
// 		}else{
        return true;
// 		}
    }

    /**
     * 参数的初始化
     */
    public function parmsInit()
    {
        // 初始化项目的绝对根路径，比如E:/program/phpService/www
        $this->absBasePath = Yii::$app->basePath;
        //项目的文件相对路径的地址，比如为/advanced/backend/web
        $this->url_base = Url::base();
        $last = '..' . DIRECTORY_SEPARATOR;
        $ds = DIRECTORY_SEPARATOR;
        //vender的相对路径
        $this->rel_vender_path = ($this->url_base)
            . $ds . $last . $last . 'vendor' . $ds;
        //vender的adminlte的浏览器的adminlte的相对路径
        $this->adminlte = $this->rel_vender_path . 'almasaeed2010' . $ds . 'adminlte' . $ds;
        //vender的adminlte的浏览器的组建的相对路径
        $this->rel_vaabc_path = $this->adminlte . 'bower_components' . $ds;
    }

    /**
     * 弹窗，弹出窗口
     * @param string $view 是视图，controller/action
     * @param array $data 是数据，['model'=>$model]
     * @return string
     */
// 	public function actionAlert($view,$data){

// 	}

    /**
     * 分页的功能操作
     */
    protected function pagesDo($dataProvider){
        $query = $dataProvider->query;
        $countQuery = clone $query;
        $pages = new Pagination(['totalCount' => $countQuery->count()]);
        if (isset($_POST['pageSize'])) {//设置了参数，就把参数给pageSize session
            setcookie('pageSize', $_POST['pageSize']);
        }
        if (!empty($_COOKIE['pageSize'])) {
            $pages->pageSize = (int)$_COOKIE['pageSize'];
        } else {
            $pages->pageSize = 2;
        }
        $dataProvider->pagination = $pages;
    }
}